package org.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

/**
 * Hello world!
 *
 */
public class ES_Doc_Insert_Batch_08 {
    public static void main( String[] args ) throws Exception {
        // 创建ES客户端
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost", 9200, "http")));
        // 批量插入数据
        BulkRequest request = new BulkRequest();

        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","张三","age",30));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","李四","age",70));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","王五1","age",10));
        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","王五2","age",60));
        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","王五3","age",30));

        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        // 响应状态
        System.out.println(response.getItems());
        System.out.println(response.getTook());
        client.close();
    }
}
